// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.awt.Dimension;
import java.awt.Point;
import zombie.core.math.PZMath;

public final class Vector2 implements Cloneable {
    public float x;
    public float y;

    public Vector2() {
        this.x = 0.0F;
        this.y = 0.0F;
    }

    public Vector2(Vector2 other) {
        this.x = other.x;
        this.y = other.y;
    }

    public Vector2(float _x, float _y) {
        this.x = _x;
        this.y = _y;
    }

    public static Vector2 fromAwtPoint(Point p) {
        return new Vector2((float)p.x, (float)p.y);
    }

    public static Vector2 fromLengthDirection(float length, float direction) {
        Vector2 vector2 = new Vector2();
        vector2.setLengthAndDirection(direction, length);
        return vector2;
    }

    public static float dot(float _x, float _y, float tx, float ty) {
        return _x * tx + _y * ty;
    }

    public static Vector2 addScaled(Vector2 a, Vector2 b, float scale, Vector2 result) {
        result.set(a.x + b.x * scale, a.y + b.y * scale);
        return result;
    }

    public void rotate(float rad) {
        double double0 = (double)this.x * Math.cos((double)rad) - (double)this.y * Math.sin((double)rad);
        double double1 = (double)this.x * Math.sin((double)rad) + (double)this.y * Math.cos((double)rad);
        this.x = (float)double0;
        this.y = (float)double1;
    }

    public Vector2 add(Vector2 other) {
        this.x = this.x + other.x;
        this.y = this.y + other.y;
        return this;
    }

    public Vector2 aimAt(Vector2 other) {
        this.setLengthAndDirection(this.angleTo(other), this.getLength());
        return this;
    }

    public float angleTo(Vector2 other) {
        return (float)Math.atan2((double)(other.y - this.y), (double)(other.x - this.x));
    }

    public float angleBetween(Vector2 other) {
        float _float = this.dot(other) / (this.getLength() * other.getLength());
        if (_float < -1.0F) {
            _float = -1.0F;
        }

        if (_float > 1.0F) {
            _float = 1.0F;
        }

        return (float)Math.acos((double)_float);
    }

    public Vector2 clone() {
        return new Vector2(this);
    }

    public float distanceTo(Vector2 other) {
        return (float)Math.sqrt(Math.pow((double)(other.x - this.x), 2.0) + Math.pow((double)(other.y - this.y), 2.0));
    }

    public float dot(Vector2 other) {
        return this.x * other.x + this.y * other.y;
    }

    public boolean equals(Object other) {
        return !(other instanceof Vector2 vector2) ? false : vector2.x == this.x && vector2.y == this.y;
    }

    public float getDirection() {
        float _float = (float)Math.atan2((double)this.y, (double)this.x);
        return PZMath.wrap(_float, (float) -Math.PI, (float) Math.PI);
    }

    public static float getDirection(float _x, float _y) {
        float _float = (float)Math.atan2((double)_y, (double)_x);
        return PZMath.wrap(_float, (float) -Math.PI, (float) Math.PI);
    }

    @Deprecated
    public float getDirectionNeg() {
        return (float)Math.atan2((double)this.x, (double)this.y);
    }

    public Vector2 setDirection(float direction) {
        this.setLengthAndDirection(direction, this.getLength());
        return this;
    }

    public float getLength() {
        return (float)Math.sqrt((double)(this.x * this.x + this.y * this.y));
    }

    public float getLengthSquared() {
        return this.x * this.x + this.y * this.y;
    }

    public Vector2 setLength(float length) {
        this.normalize();
        this.x *= length;
        this.y *= length;
        return this;
    }

    public float normalize() {
        float _float = this.getLength();
        if (_float == 0.0F) {
            this.x = 0.0F;
            this.y = 0.0F;
        } else {
            this.x /= _float;
            this.y /= _float;
        }

        return _float;
    }

    public Vector2 set(Vector2 other) {
        this.x = other.x;
        this.y = other.y;
        return this;
    }

    public Vector2 set(float _x, float _y) {
        this.x = _x;
        this.y = _y;
        return this;
    }

    public Vector2 setLengthAndDirection(float direction, float length) {
        this.x = (float)(Math.cos((double)direction) * (double)length);
        this.y = (float)(Math.sin((double)direction) * (double)length);
        return this;
    }

    public Dimension toAwtDimension() {
        return new Dimension((int)this.x, (int)this.y);
    }

    public Point toAwtPoint() {
        return new Point((int)this.x, (int)this.y);
    }

    public String toString() {
        return String.format("Vector2 (X: %f, Y: %f) (L: %f, D:%f)", this.x, this.y, this.getLength(), this.getDirection());
    }

    public float getX() {
        return this.x;
    }

    public void setX(float _x) {
        this.x = _x;
    }

    public float getY() {
        return this.y;
    }

    public void setY(float _y) {
        this.y = _y;
    }

    public void tangent() {
        double double0 = (double)this.x * Math.cos(Math.toRadians(90.0)) - (double)this.y * Math.sin(Math.toRadians(90.0));
        double double1 = (double)this.x * Math.sin(Math.toRadians(90.0)) + (double)this.y * Math.cos(Math.toRadians(90.0));
        this.x = (float)double0;
        this.y = (float)double1;
    }

    public void scale(float scale) {
        scale(this, scale);
    }

    public static Vector2 scale(Vector2 val, float scale) {
        val.x *= scale;
        val.y *= scale;
        return val;
    }
}
